using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using net_work.Common.Request;
using net_work.Common.Result;
using net_work.Data.DTO.Cou;
using net_work.IService.Cou;

namespace net_work.Controllers.Cou;

[Route("api/[controller]")]
[ApiController]
public class TeachingController(ILogger<TeachingController> logger, ITeachingService teachingService) : ControllerBase
{
    /// <summary>
    /// 获取教学列表
    /// </summary>
    /// <param name="request">分页请求实体</param>
    /// <returns>操作结果</returns>
    [HttpPost("list")]
    [Authorize]
    public async Task<IActionResult> ListTeachings([FromBody] PagedRequest request)
    {
        logger.LogInformation("获取教学列表");
        var result = await teachingService.GetTeachingsPagedAsync(request);
        return AjaxResult.Success(result);
    }

    /// <summary>
    /// 获取教学信息
    /// </summary>
    /// <param name="teachingId">教学ID</param>
    /// <returns>操作结果</returns>
    [HttpGet("get")]
    [Authorize]
    public async Task<IActionResult> GetTeachingById([FromQuery] long teachingId)
    {
        logger.LogInformation("获取教学信息");
        return await teachingService.SelectByIdAsync(teachingId) is { } teaching
            ? AjaxResult.Success(teaching)
            : AjaxResult.Error();
    }

    /// <summary>
    /// 新增教学
    /// </summary>
    /// <param name="teachingAddRequest">新增教学实体</param>
    /// <returns>操作结果</returns>
    [HttpPost("add")]
    [Authorize(Roles = "ADMIN,SECRET")]
    public async Task<IActionResult> AddTeaching([FromBody] TeachingAddRequest teachingAddRequest)
    {
        logger.LogInformation("新增教学");
        var teachingId = await teachingService.InsertTeaching(teachingAddRequest);
        return teachingId > 0
            ? AjaxResult.Success(new { teachingId })
            : AjaxResult.Error();
    }

    /// <summary>
    /// 修改教学
    /// </summary>
    /// <param name="teachingUpdateRequest">修改教学实体</param>
    /// <returns>操作结果</returns>
    [HttpPost("update")]
    [Authorize(Roles = "ADMIN,SECRET")]
    public async Task<IActionResult> UpdateTeaching([FromBody] TeachingUpdateRequest teachingUpdateRequest)
    {
        logger.LogInformation("修改教学");
        return await teachingService.UpdateTeaching(teachingUpdateRequest) > 0
            ? AjaxResult.Success()
            : AjaxResult.Error();
    }

    /// <summary>
    /// 删除教学
    /// </summary>
    /// <param name="ids">教学ID</param>
    /// <returns>操作结果</returns>
    [HttpPost("delete")]
    [Authorize(Roles = "ADMIN,SECRET")]
    public async Task<IActionResult> DeleteTeaching([FromBody] List<long> ids)
    {
        logger.LogInformation("删除教学");
        return await teachingService.DeleteTeachingByIds(ids) > 0
            ? AjaxResult.Success()
            : AjaxResult.Error();
    }

    /// <summary>
    /// 发布教学
    /// </summary>
    /// <param name="teachingId">教学ID</param>
    /// <returns>操作结果</returns>
    [HttpPost("release")]
    [Authorize(Roles = "ADMIN,SECRET")]
    public async Task<IActionResult> Release([FromBody] long teachingId)
    {
        return await teachingService.Release(teachingId) > 0
            ? AjaxResult.Success()
            : AjaxResult.Error();
    }
    
    /// <summary>
    /// 导出教学班
    /// </summary>
    /// <returns>操作结果</returns>
    [HttpGet("export")]
    [Authorize(Roles = "ADMIN,SECRET")]
    public async Task<IActionResult> ExportTeaching()
    {
        logger.LogInformation("导出教学班");
        var fileBytes = await teachingService.ExportTeaching();
        if (fileBytes.Length == 0)
        {
            return AjaxResult.Error("导出失败");
        }

        return File(fileBytes, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "教学表.xlsx");
    }
}